//
//  CustomSliderStyle.swift
//  xgwjinterconnection
//
//  Created by WangWeiShuo on 2025/8/19.
//
import SwiftUI

struct CustomSlider: View {
    @Binding var value: Double
    let range: ClosedRange<Double>
    let trackHeight: CGFloat
    let thumbSize: CGFloat
    let trackColor: Color
    let progressColor: Color
    let thumbColor: Color
    
    init(value: Binding<Double>,
         in range: ClosedRange<Double> = 0...1,
         trackHeight: CGFloat = 10,
         thumbSize: CGFloat = 25,
         trackColor: Color = .gray.opacity(0.3),
         progressColor: Color = .blue,
         thumbColor: Color = .white) {
        self._value = value
        self.range = range
        self.trackHeight = trackHeight
        self.thumbSize = thumbSize
        self.trackColor = trackColor
        self.progressColor = progressColor
        self.thumbColor = thumbColor
    }
    
    var body: some View {
        GeometryReader { geometry in
            ZStack(alignment: .leading) {
                // 背景轨道
                RoundedRectangle(cornerRadius: trackHeight / 2)
                    .fill(trackColor)
                    .frame(height: trackHeight)
                
                // 进度轨道
                RoundedRectangle(cornerRadius: trackHeight / 2)
                    .fill(progressColor)
                    .frame(width: progressWidth(geometry), height: trackHeight)
                
                // 拇指
                Circle()
                    .fill(thumbColor)
                    .frame(width: thumbSize, height: thumbSize)
                    .shadow(color: .black.opacity(0.2), radius: 3, x: 0, y: 1)
                    .overlay(Circle().stroke(Color.black.opacity(0.1), lineWidth: 1)
                    .offset(x: thumbPosition(geometry) - thumbSize / 2)
                    .gesture(
                        DragGesture(minimumDistance: 0)
                            .onChanged { gestureValue in
                                updateValue(with: gestureValue, in: geometry)
                            }
                    )
                             )
            }
            .frame(height: max(trackHeight, thumbSize))
        }
        .frame(height: max(trackHeight, thumbSize))
    }
    
    private func progressWidth(_ geometry: GeometryProxy) -> CGFloat {
        let availableWidth = geometry.size.width - thumbSize
        let normalizedValue = (value - range.lowerBound) / (range.upperBound - range.lowerBound)
        return max(0, min(availableWidth, availableWidth * CGFloat(normalizedValue))) + thumbSize / 2
    }
    
    private func thumbPosition(_ geometry: GeometryProxy) -> CGFloat {
        let availableWidth = geometry.size.width - thumbSize
        let normalizedValue = (value - range.lowerBound) / (range.upperBound - range.lowerBound)
        return CGFloat(normalizedValue) * availableWidth + thumbSize / 2
    }
    
    private func updateValue(with gesture: DragGesture.Value, in geometry: GeometryProxy) {
        let availableWidth = geometry.size.width - thumbSize
        let xLocation = min(max(0, gesture.location.x), availableWidth)
        let normalizedValue = Double(xLocation / availableWidth)
        value = normalizedValue * (range.upperBound - range.lowerBound) + range.lowerBound
    }
}

// 预览
struct CustomSlider_Previews: PreviewProvider {
    static var previews: some View {
        VStack(spacing: 30) {
            Text("自定义高度滑块示例")
                .font(.title)
                .padding()
            
            VStack {
                Text("默认高度滑块")
                CustomSlider(value: .constant(0.3))
                    .padding(.horizontal)
            }
            
            VStack {
                Text("高滑块 (30pt)")
                CustomSlider(value: .constant(0.6), trackHeight: 30)
                    .padding(.horizontal)
            }
            
            VStack {
                Text("自定义颜色滑块")
                CustomSlider(
                    value: .constant(0.8),
                    trackHeight: 15,
                    thumbSize: 30,
                    trackColor: .orange.opacity(0.2),
                    progressColor: .orange,
                    thumbColor: .red
                )
                .padding(.horizontal)
            }
        }
        .padding()
    }
}
